package com.dingreading.cloud.ssxcx.controller;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.aliyun.MstCdnUtil;
import com.dingreading.cloud.ssxcx.dto.request.StudentAnswerRequestDto;
import com.dingreading.cloud.ssxcx.service.XcxStudentQuizAnswersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/xcx/studentQuizAnswers")
@Api(tags = "小程序-学生课堂巩固")
public class XcxStudentQuizAnswersController {
    @Resource
    private XcxStudentQuizAnswersService xxStudentQuizAnswersService;

    @ApiOperation(value = "获取当前学生课中答题是否全部正确")
    @PostMapping(value = "/checkCorrect", produces = "application/json; charset=UTF-8")
    public R<Object> checkCorrect(
            @ApiParam(value = "伙伴uid") @RequestParam(value = "agencyUid") String agencyUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "学生studentUid") @RequestParam(value = "studentUid") String studentUid,
            @ApiParam(value = "课节uid") @RequestParam(value = "subjectLessonUid") String subjectLessonUid
    ) {
        R<Object> ret = xxStudentQuizAnswersService.checkCorrect(agencyUid, storeUid, studentUid, subjectLessonUid);
        return ret;
    }

    @ApiOperation(value = "学生课中答题批改")
    @PostMapping(value = "/submitBatchAnswers", produces = "application/json; charset=UTF-8")
    public R<Object> submitBatchStudentQuizAnswers(
            @ApiParam(value = "学生提交答题") @RequestBody StudentAnswerRequestDto requestDto
    ) {
        return xxStudentQuizAnswersService.submitStudentQuizAnswers(
                requestDto.getAgencyUid(), requestDto.getStoreUid(),
                requestDto.getSubjectLessonUid(), requestDto.getStudentUid(), requestDto.getOptionItems()
        );
    }

    @ApiOperation(value = "根据错题uid获取题目")
    @PostMapping(value = "/getMistakeQuestions", produces = "application/json; charset=UTF-8")
    public R<Object> getMistakeQuestions(
            HttpServletRequest request,
            @ApiParam(value = "伙伴uid") @RequestParam(value = "agencyUid") String agencyUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "学生studentUid") @RequestParam(value = "studentUid") String studentUid,
            @ApiParam(value = "课节uid") @RequestParam(value = "subjectLessonUid") String subjectLessonUid,
            @ApiParam(value = "错题uid集合") @RequestParam(value = "questionUids") List<String> questionUids
    ) {
        R<Object> r = MstCdnUtil.getIp(request);
        if (R.isError(r))
            return r;
        String ip = (String) r.getData();

        return xxStudentQuizAnswersService.getMistakeQuestions(agencyUid, storeUid, studentUid, subjectLessonUid, questionUids, ip);
    }

}
